package com.cn.parks.servlet;

import com.cn.parks.dao.ParkingSeatDao;
import com.cn.parks.dao.TempParkingDao;
import com.cn.parks.dao.UserDao;
import com.cn.parks.dao.impl.ParkingSeatDaoImpl;
import com.cn.parks.dao.impl.TempParkingDaoImpl;
import com.cn.parks.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/dashboardStats")
public class DashboardStatsServlet extends HttpServlet {
    private UserDao userDao = new UserDaoImpl();
    private ParkingSeatDao parkingSeatDao = new ParkingSeatDaoImpl();
    private TempParkingDao tempParkingDao = new TempParkingDaoImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String totalSeats = null;
        String availableSeats = null;
        String userCount = null;
        String todayIncome = null;
        try {
            userCount = userDao.getUserCount().toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            availableSeats = parkingSeatDao.getAvailableCount().toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            totalSeats = parkingSeatDao.getParkCount().toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try {
            todayIncome = tempParkingDao.getTodayIncome().toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("totalSeats: " + totalSeats);
        System.out.println(
                "availableSeats: " + availableSeats
        );
        System.out.println("totalMembers: " + userCount);
        System.out.println("todayIncome: " + todayIncome);
        // 这里应该从数据库获取实际数据
        String stats = "{\"success\":true,\"data\":{" +
                "\"totalSeats\":" + totalSeats + "," +
                "\"availableSeats\":" + availableSeats + "," +
                "\"totalMembers\":" + userCount + "," +
                "\"todayIncome\":" + todayIncome +
                "}}";

        response.getWriter().write(stats);
    }
}
